package com.qzw.security.service.service.impl;

import com.qzw.common.data.BaseMysqlDao;
import com.qzw.common.service.BaseServiceImpl;
import com.qzw.security.api.model.Role;
import com.qzw.security.api.query.RoleQuery;
import com.qzw.security.api.service.IRoleService;
import com.qzw.security.service.dao.RoleDao;
import com.qzw.security.service.dao.UserDao;
import com.qzw.security.service.entity.SysMenu;
import com.qzw.security.service.entity.SysRole;
import com.qzw.security.service.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.transaction.Transactional;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * @author ：quziwei
 * @date ：Created in 2020/6/2 11:56 上午
 * @description：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role, SysRole,Integer, RoleQuery> implements IRoleService {
    @Autowired
    private RoleDao dao;
    @Autowired
    private UserDao userDao;

    @Override
    public Role update(Role model) {
        if (isNotEmpty(model.getId())) {
            Optional<SysRole> sysRoleOptional = dao.findById(model.getId());
            SysRole sysRole = sysRoleOptional.get();
            if (isNotEmpty(model.getName()))
                sysRole.setName(model.getName());
            if (isNotEmpty(model.getDescription()))
                sysRole.setDescription(model.getDescription());
            if (isNotEmpty(model.getStatus()))
                sysRole.setStatus(model.getStatus());

            model = entity2model(dao.save(sysRole));
        } else {
            model = entity2model(dao.save(model2Entity(model)));
        }
        return model;
    }

    @Override
    @Transactional
    public void menuEdit(Integer roleId, List<Integer> menuIds) {
        Optional<SysRole> sysRole = dao.findById(roleId);
        Set<SysMenu> menus = new HashSet<>();
        sysRole.get().setMenus(menus);
        if (!CollectionUtils.isEmpty(menuIds)) {
            for (Integer menuId : menuIds) {
                SysMenu sysMenu = new SysMenu();
                sysMenu.setId(menuId);
                menus.add(sysMenu);
            }
        }
        dao.save(sysRole.get());
    }

    @Override
    @Transactional
    public void menuAdd(Integer roleId, Integer menuId) {
        Optional<SysRole> sysRole = dao.findById(roleId);
        Set<SysMenu> menus = sysRole.get().getMenus();
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(menuId);
        menus.add(sysMenu);
        dao.save(sysRole.get());
    }

    @Override
    @Transactional
    public List<Role> findRolesByUserId(Integer id) {
        Optional<SysUser> sysUser = userDao.findById(id);
        Set<SysRole> roles = sysUser.get().getRoles();
        return entity2Model(roles);
    }

    @Override
    protected BaseMysqlDao<SysRole, Integer> getDao() {
        return dao;
    }

    @Override
    protected SysRole model2Entity(Role model) {
        SysRole sysRole = null;
        if (!ObjectUtils.isEmpty(model)) {
            sysRole = new SysRole();
            sysRole.setCreateTime(model.getCreateTime());
            sysRole.setCreateUserId(model.getCreateUserId());
            sysRole.setDescription(model.getDescription());
            sysRole.setId(model.getId());
            sysRole.setName(model.getName());
            sysRole.setStatus(model.getStatus());
        }
        return sysRole;
    }

    @Override
    protected Role entity2model(SysRole entity) {
        Role role = null;
        if (!ObjectUtils.isEmpty(entity)) {
            role = new Role();
            role.setCreateTime(entity.getCreateTime());
            role.setCreateUserId(entity.getCreateUserId());
            role.setDescription(entity.getDescription());
            role.setId(entity.getId());
            role.setName(entity.getName());
            role.setStatus(entity.getStatus());
        }
        return role;
    }
}
